<?php
//我的任务
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['set']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$id = intval($_RQ['id']);
	$rid = intval($_RQ['rid']);	
	if (!(empty($rid))) {
		$p=1;
		$sql = 'select * from ' . tablename('jy_renwu_task_record') . ' where id = :id and uniacid = :uniacid';
		$detail = pdo_fetch($sql, array(':id' => $rid, ':uniacid' => $_SESSION['uniacid']));
		$reward = json_decode($detail['reward_data'], true);
		if($reward['reward_coupon']){
				$reward['coupon']=pdo_get('jy_weishop_coupon',array('id'=>$reward['reward_coupon'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($reward['reward_goods']){
				$reward['goods']=pdo_get('jy_weishop_goods',array('id'=>$reward['reward_goods'],'uniacid'=>$_SESSION['uniacid']));
		}
		$reward_goods = pdo_fetchall('select * from ' . tablename('jy_renwu_task_reward') . ' where recordid = ' . $detail['id'] . ' and mid = \'' . $_SESSION['uid'] . '\' and uniacid = ' . $_SESSION['uniacid'] . ' and isjoiner = 0 and reward_type = \'reward_goods\' and `level` = 0');
		$reward1 = $reward2 = $reward3 = array();
		$reward_goods1 = $reward_goods2 = $reward_goods3 = array();
		if ($detail['tasktype'] == 'poster') {
			if ($detail['level2'] == 0) {
				$detail['level1'] = $detail['task_demand'];
				$reward1 = $reward;
				$reward_goods1 = $reward_goods;
			}
			else if (($detail['level2'] < $detail['task_demand']) && ($detail['level1'] < $detail['task_demand'])) {
				$reward1 = json_decode($detail['reward_data1'], true);
				$reward_goods1 = pdo_fetchall('select * from ' . tablename('jy_renwu_task_reward') . ' where recordid = ' . $detail['id'] . ' and mid = \'' . $_SESSION['uid'] . '\' and uniacid = ' . $_SESSION['uniacid'] . ' and isjoiner = 0 and reward_type = \'goods\' and `level` = 1');
				$reward2 = json_decode($detail['reward_data2'], true);
				$reward_goods2 = pdo_fetchall('select * from ' . tablename('jy_renwu_task_reward') . ' where recordid = ' . $detail['id'] . ' and mid = \'' . $_SESSION['uid'] . '\' and uniacid = ' . $_SESSION['uniacid'] . ' and isjoiner = 0 and reward_type = \'goods\' and `level` = 2');
				$reward3 = $reward;
				$reward_goods3 = $reward_goods;
				$detail['level3'] = $detail['task_demand'];
			}
			else {
				$reward1 = json_decode($detail['reward_data1'], true);
				
				$reward_goods1 = pdo_fetchall('select * from ' . tablename('jy_renwu_task_reward') . ' where recordid = ' . $detail['id'] . ' and mid = \'' . $_SESSION['uid'] . '\' and uniacid = ' . $_SESSION['uniacid'] . ' and isjoiner = 0 and reward_type = \'goods\' and `level` = 1');
				$reward1['goods']=pdo_get('jy_weishop_goods',array('id'=>$reward1['reward_goods'],'uniacid'=>$_SESSION['uniacid']));
			//	print_r($reward1['goods']);exit;
				$reward2 = $reward;
				$reward_goods2 = $reward_goods;
			}
		}
			$followreward = json_decode($detail['followreward_data'], true);
			$joiner = pdo_fetchall('select DISTINCT mid,headimg,nickname,gettime from ' . tablename('jy_renwu_task_reward') . ' where isjoiner = 1 and recordid = ' . $rid . ' and `get`=1 and uniacid = ' . $_SESSION['uniacid']);
		}
		if (empty($detail) && !(empty($id))) {
			$p=2;
			$sql = 'select * from ' . tablename('jy_renwu_task_list') . ' where id = :id and uniacid = :uniacid';
			$detail = pdo_fetch($sql, array(':id' => $id, ':uniacid' => $_SESSION['uniacid']));
			$reward = json_decode($detail['reward'], true);
			if($reward['reward_coupon']){
				$reward['coupon']=pdo_get('jy_weishop_coupon',array('id'=>$reward['reward_coupon'],'uniacid'=>$_SESSION['uniacid']));
			}
			if ($detail['type'] == 'poster') {
				$detail['tasktype'] = 'poster';
				$detail['level1'] = $detail['demand'];
				$detail['demand'] = max($detail['demand'], $detail['level2'], $detail['level3']);

				if ($detail['level2'] == 0) {
					$reward1 = $reward;
				}
				 else if (0 < $detail['level3']) {
					$reward1 = $reward;
					$reward2 = json_decode($detail['reward2'], true);
					$reward3 = json_decode($detail['reward3'], true);
				}
				 else if (0 < $detail['level2']) {
					$reward1 = $reward;
					$reward2 = json_decode($detail['reward2'], true);
				}

			}


			$followreward = json_decode($detail['followreward'], true);
		}
		if (empty($detail)) {
			exi('任务不存在','error');
		}
		!(empty($detail['tasktype'])) && ($type = $detail['tasktype']);
		!(empty($detail['type'])) && ($type = $detail['type']);
		$taskType = getTaskType($type);
		$desc = $taskType['verb'];
		if (!(empty($taskType['unit']))) {
			$desc .= $detail['task_demand'] . $detail['demand'] . $taskType['unit'];
		}
		if (isset($detail['tasktype']) && ($detail['tasktype'] == 'poster')) {		
			$user=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			$updata['nickname']=$user['nickname'];
			$updata['avatar']=$user['avatar'];
			if($p==1){
				$posterid=pdo_getcolumn('jy_renwu_task_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$detail['taskid']),'posterid');
				
			}elseif($p==2){
				$posterid=$detail['posterid'];
			}
			$item=pdo_get('jy_poster_poster',array('id'=>$posterid,'uniacid'=>$_SESSION['uniacid']));
			$data=json_decode($item['data'],true);
			$poster['id']=$item['id'].'_'.$id;
			$poster['data']=$data['data'];
			$poster['bg']=$data['image'];
			$url=custom_url('jy_renwu/index//id='.$id,true);
			$qr=cfc('tools')->createqrcode($url,150,true);
			$img=cfc('jy_poster')->createPoster($poster,$qr,$updata,true,'','jy_renwu');
	}
	set_html('title','任务详情');
	//获取token
	$token = $_SESSION['token'];
	$return['rid']=$rid;
	$return['token']=$token;
	$return['img']=$img;
	$return['desc']=$desc;
	$return['detail']=$detail;
	$return['reward']=$reward;
	$return['reward_goods']=$reward_goods;
	$return['reward1']=$reward1;
	$return['reward_goods1']=$reward_goods1;
	$return['reward2']=$reward2;
	$return['reward3']=$reward3;
	$return['reward_goods2']=$reward_goods2;
	$return['reward_goods3']=$reward_goods3;
	$return['followreward']=$followreward;
	$return['joiner']=$joiner;
	//print_r($detail['reward2']);exit;
	exi($return);
}
if($_RQ['op']=='send1'){
	$url=custom_url('jy_renwu/index/',true);
	$postdata=get_postdata();
//	print_r($postdata['img']);exit;
	$newsmsg['msgtype']='news';
	$articles[0]['title'] = "";
	//$articles[0]['description']=$postdata['img'];
	$articles[0]['URL'] =$url;
	$articles[0]['picurl'] =$postdata['img'];
	$newsmsg['news']['articles'] =$articles;
	cfc('account')->sendCustomNotice($newsmsg,$_SESSION['uid']);
	$return['ok']=1;
	exi($return);
}
if($_RQ['op']=='send'){
	$postdata=get_postdata();
	$openid=pdo_getcolumn('core_fans',array('mid'=>$_SESSION['uid']),'openid');
	$date['msgtype']='image';
	$date['touser']=$openid;
	$accesstoken=cfc('account')->getAccessToken();
	$date['image']['media_id']=cfc('account')->add_media($postdata['img'],'image');
	$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$accesstoken;
	$data=ijson_encode($data);
	cfc('account')->request($url,$data,true,false,true);
	$return['ok']=1;
	exi($return);
}

if($_RQ['op']=='getReward'){
	$postdata=get_postdata();
	$detail_id=$postdata['detail_id'];
	$detail=pdo_get('jy_renwu_task_record',array('id'=>$detail_id));
	$task=pdo_get('jy_renwu_task_list',array('id'=>$detail['taskid']));
	$rewards=pdo_getall('jy_renwu_task_reward',array('uniacid'=>$_SESSION['uniacid'],'recordid'=>$detail_id,'mid'=>$_SESSION['uid'],'sent'=>'0'));
	//$reward_data=json_decode($detail['reward_data'],true);
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$openid=pdo_getcolumn('core_fans',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'openid');
	foreach($rewards as $reward){
		if($reward['reward_type']=='credit1'){
			
			pdo_update('core_members',array('credit1'=>$member['credit1']+$reward_data['credit1']),array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		
		
		}elseif($reward['reward_type']=='credit2'){
				pdo_update('core_members',array('credit1'=>$member['credit2']+$reward_data['credit2']),array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
				
		}elseif($reward['reward_type']=='red'){
				$params=array(
					'mch_billno'=>date("Ymd",time()).date("His",time()).rand(1111,9999),
					'openid'=>$openid,
					'money'=>$reward['reward_data'],
					'remark'=>'活动红包',
					'act_name'=>$task['title'].'红包',
					);
				cfc('wechat_pay')->redpack($params);
				
		}elseif($reward['reward_type']=='reward_coupon'){
			$params=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'couponid'=>$reward['reward_data'],
				'createtime'=>TIMESTAMP,
				'isnew'=>1
				);
				pdo_insert('jy_weishop_coupon_getlog',$params);
				
		}
		pdo_update('jy_renwu_task_reward',array('get'=>'1','sent'=>1,'gettime'=>date('Y-m-d H:i:s'),'senttime'=>date('Y-m-d H:i:s')),array('id'=>$reward['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	$return['ok']=1;
	exi($return);
}
